package org.hackystat.sdsa.analyzer.model.activity.refactoring;

import java.util.HashMap;

import org.hackystat.sdsa.analyzer.model.activity.refactoring.RefactorOperator.Operator;

/**
 * Defines refactoring subject.
 *  
 * @author Hongbing Kou
 */
public class RefactorSubjectType {
  /** Subject type enumerator. */
  public enum SubjectType { CLASS, METHOD, FIELD, IMPORT, UNKNOWN };

  /** Subject type. */
  private SubjectType subType;
  
  /** Gets the set of refactor subject. */
  private static HashMap<String, RefactorSubjectType> subjectTypeMap;
  
  static {
    subjectTypeMap = new HashMap<String, RefactorSubjectType>();
    for (SubjectType subType : SubjectType.values()) {
      subjectTypeMap.put(subType.name(), new RefactorSubjectType(subType));
    }
  }
  
  /**
   * Constructs a RefactorSubject.  
   * 
   * @param subType Subject type.
   */
  private RefactorSubjectType(SubjectType subType) {
    this.subType = subType;    
  }

  /**
   * Gets refactor subject name.
   * 
   * @return Refactor subject name.
   */
  public String getName() {
    return this.subType.name();
  }

  /**
   * Gets refactor subject instance.
   * 
   * @param type Refactor subject type string.
   * @return Refactor subject type.
   */
  public static RefactorSubjectType getInstance(String type) {
    String upperType = type.toUpperCase();
    
    if (subjectTypeMap.containsKey(upperType)) {
      return subjectTypeMap.get(upperType);
    }
    else {
      return subjectTypeMap.get(Operator.UNKNOWN.name());
    }
  }
  
  /**
   * String representation of subject type.
   * 
   * @return Subject type string.
   */
  @Override public String toString() {
    return this.subType.name();
  }
}